package com.tnovoselec.android.lbs.map;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.OverlayItem;
import com.tnovoselec.android.lbs.R;


public class BalloonView<E extends OverlayItem> extends FrameLayout {

	protected final LinearLayout layout;
	private final TextView title;

	private final TextView snippet;

	// TODO Put resources and layouts in constructors also!
	public BalloonView(Context context, int balloonBottomOffset) {
		super(context);
		super.setPadding(10, 0, 10, balloonBottomOffset);

		layout = new LinearLayout(context);
		layout.setVisibility(View.VISIBLE);

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.balloon_overlay, layout);
		title = (TextView) v.findViewById(R.id.balloon_item_title);
		snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);

		ImageView close = (ImageView) v.findViewById(R.id.close_img_button);
		close.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(@SuppressWarnings("hiding") View v) {
				layout.setVisibility(View.GONE);
			}
		});

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;

		super.addView(layout, params);
	}

	public void setData(E item) {
		layout.setVisibility(View.VISIBLE);

		if (item.getTitle() != null) {
			title.setVisibility(View.VISIBLE);
			title.setText(item.getTitle());
		} else {
			title.setVisibility(View.GONE);
		}

		if (item.getSnippet() != null) {
			snippet.setVisibility(View.VISIBLE);
			snippet.setText(item.getSnippet());
		} else {
			snippet.setVisibility(View.GONE);
		}
	}
}
